# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 11, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
# SUBSET: ONLY NON-HISPANIC WHITE RESPONDENTS. 
our.data = subset(our.data, whiteNH==1)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# DATAFRAME: REPUBLICANS
attach(our.data)
gop.data = data.frame(matchedGOP_affirmativeaction, matchedGOP_aidblacks,
                      matchedGOP_RRdifficultsucceed, matchedGOP_RRlessthandeserve,
                      matchedGOP_RRnofavors, matchedGOP_RRtryharder, 
                      year, VCF0009z, parties3)
gop.data = subset(gop.data, parties3=="Republican")
detach(our.data)

# DATAFRAME: DEMOCRATS
attach(our.data)
dems.data = data.frame(matchedDEM_affirmativeaction, matchedDEM_aidblacks, 
                       matchedDEM_RRdifficultsucceed, matchedDEM_RRlessthandeserve,
                       matchedDEM_RRnofavors, matchedDEM_RRtryharder, 
                       year, VCF0009z, parties3)
dems.data = subset(dems.data, parties3=="Democrat")
detach(our.data)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# DESIGNS
gop.design = svydesign(ids=~1, weight=~gop.data$VCF0009z,
                       data = gop.data)
dems.design = svydesign(ids=~1, weight=~dems.data$VCF0009z,
                        data = dems.data)

# FOR LOOP
our.means.GOP = list(list())
our.props.GOP = list(list())
our.means.DEM = list(list())
our.props.DEM = list(list())
for(i in 1:6){
  # REPUBLICANS
  our.means.GOP[[i]] = svytable(~ gop.data[, i] + year, 
                                design = gop.design)
  our.props.GOP[[i]] = prop.table(our.means.GOP[[i]], 2)
  our.props.GOP[[i]]
  our.props.GOP[[i]] = as.data.frame(our.props.GOP[[i]])
  our.props.GOP[[i]] = subset(our.props.GOP[[i]], our.props.GOP[[i]][, 1]==1 & Freq!=0)
  names(our.props.GOP[[i]])[1] = "outcome"
  # DEMOCRATS 
  our.means.DEM[[i]] = svytable(~ dems.data[, i] + year, 
                                design = dems.design)
  our.props.DEM[[i]] = prop.table(our.means.DEM[[i]], 2)
  our.props.DEM[[i]]
  our.props.DEM[[i]] = as.data.frame(our.props.DEM[[i]])
  our.props.DEM[[i]] = subset(our.props.DEM[[i]], our.props.DEM[[i]][, 1]==1 & Freq!=0)
  names(our.props.DEM[[i]])[1] = "outcome"
}

# PLOTTING DATA
newer.data = rbind(our.props.GOP[[1]], 
                   our.props.GOP[[2]], 
                   our.props.GOP[[3]],
                   our.props.GOP[[4]], 
                   our.props.GOP[[5]], 
                   our.props.GOP[[6]],
                   our.props.DEM[[1]], 
                   our.props.DEM[[2]], 
                   our.props.DEM[[3]],
                   our.props.DEM[[4]], 
                   our.props.DEM[[5]], 
                   our.props.DEM[[6]])
newer.data = newer.data[-c(1)]
newer.data$parties = c(rep("Republicans", 78),
                       rep("Democrats", 78))
newer.data$issue = rep(c(rep("Affirmative action", length(our.props.GOP[[1]]$Freq)),
                         rep("Aid to Blacks", length(our.props.GOP[[2]]$Freq)),
                         rep("Racial resentment: past discrim. \nimpairs Black success", length(our.props.GOP[[3]]$Freq)),
                         rep("Racial resentment: Blacks receive \nless than deserved", length(our.props.GOP[[4]]$Freq)),
                         rep("Racial resentment: \nno special favors", length(our.props.GOP[[5]]$Freq)),
                         rep("Racial resentment: Blacks \nshould try harder", length(our.props.GOP[[6]]$Freq))), 2)
newer.data$year = as.numeric(as.character(newer.data$year))
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# FIGURE 11: PARTY COHESION ON RACIAL ATTITUDES: MATCHED AND ISSUE-SORTED 
# WHITES IN THE PARTY COALITIONS, 1972-2020. 
our.plot = ggplot(newer.data,
                  aes(year, Freq, group = parties, 
                      linetype =  parties)) +
  geom_line(aes(linetype = parties), linewidth = 1) +
  labs(linetype = "Party ID") +  
  xlab("") +
  ylab("Proportion\n") + 
  # ggtitle("Party Cohesion on Racial Attitudes: Matched and \nIssue-Sorted Whites in the Party Coalitions, 1972-2020", 
  #        subtitle = "Data: ANES cumulative file, White partisan identifiers only. Estimates indicate the proportion of \nWhite Republican or Democratic identifiers that matched their party's ideological \norientation and shared their party's position.") +
  theme_minimal() +  
  # scale_color_grey(start = 0.8, end = 0.2) +
  facet_wrap(~ issue, ncol = 2, scales = "free_x") +
  # theme(legend.title = element_text(face = "bold")) +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9))  +
  theme(panel.spacing = unit(1, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure11.pdf", width = 6.6, height = 9)
